#include "Logaritm.h"
#include "Impartit.h"
#include "LogaritmN.h"
#include "Inmultit.h"

long double Logaritm::calcul(long double aux1, long double aux2)
{
    return Impartit::calcul(LogaritmN::calcul(aux2), LogaritmN::calcul(aux1));
}

long double Logaritm::calcul()
{
    long double aux1, aux2;

    if (!fStang || !fDrept)
        exit(1);

    aux1 = fStang->calcul();
    aux2 = fDrept->calcul();

    return calcul(aux1, aux2);
}

char *Logaritm::derivare()
{
    char *aux, *aux11, *aux2, *aux22, *aux00, *aux01;

    if (!fStang)
        exit(1);

    aux11 = fStang->toString();
    aux22 = fDrept->toString();
    aux2 = fDrept->derivare();

    aux00 = LogaritmN::concatTermeni(aux11);
    aux01 = Inmultit::concatTermeni(aux00, aux22);
    aux = Impartit::concatTermeni(aux2, aux01);

    delete aux11; delete aux22; delete aux2; delete aux00; delete aux01;

    return aux;
}

long double Logaritm::calculDerivata()
{
    if (!fStang || !fDrept)
        exit(1);

    return Impartit::calcul(fDrept->calculDerivata(), Inmultit::calcul(
                    LogaritmN::calcul(fStang->calcul()), fDrept->calcul()));
}

char *Logaritm::toString()
{
    char *aux1, *aux2, *aux;

    if (!fStang || !fDrept)
        exit(1);

    aux1 = fStang->toString();
    aux2 = fDrept->toString();
    aux = concatTermeni(aux1, aux2);
    delete aux1;

    return aux;
}

char *Logaritm::concatTermeni(char *aux1, char *aux2)
{
    char *aux;

    if (!strcmp(aux1, "1")) aux = cpystr((char*)"0");
    else
    {
        aux = new char[strlen(aux1) + strlen(aux2) + 9];
        strcpy(aux, "(log(");
        strcat(aux, aux1); strcat(aux, ","); strcat(aux, aux2); strcat(aux, "))");
    }

    return aux;
}
